/**
 * 
 */
package com.xunchi.haichuan.data.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.netai.safedoc.common.dao.BaseEntityDAOImpl;
import com.xunchi.haichuan.data.model.Data;
import com.xunchi.haichuan.utility.Constants;

/**
 * 作者：kongxb<br>
 * 模块名称：数据管理模块<br>
 * 功能描述：数据存取实现<br>
 * 维护记录：2009.11.27, kongxb 创建该类
 */
public class DataDAOImpl extends BaseEntityDAOImpl<Data> implements DataDAO {

	public DataDAOImpl() {

	}

	public DataDAOImpl(Class<Data> type) {
		super(type);
	}

	/**
	 * @see com.xunchi.haichuan.data.dao.DataDAO#getLatestDatas(int, int)
	 */
	public List<Data> getLatestDatas(int start, int limit) {
		log.debug("getting Data instance");
		try {
			DetachedCriteria dc = getEntityDetachedCriteria();
			Criteria criteria = dc.getExecutableCriteria(getSession());
			return criteria.addOrder(Order.desc("submitTime")).setFirstResult(start).setMaxResults(limit).list();
		} catch (RuntimeException re) {
			log.error("getting Data instance failed", re);
			throw re;
		}
	}

	/**
	 * @see com.xunchi.haichuan.data.dao.DataDAO#search(com.xunchi.haichuan.data.model.Data, java.util.Date,
	 *      java.util.Date)
	 */
	public List<Data> search(Data instance, Date beginDate, Date endDate) {
		log.debug("searching Data instance");
		try {
			DetachedCriteria dc = getEntityDetachedCriteria();
			if (StringUtils.isNotEmpty(instance.getProvince())) {
				dc.add(Restrictions.like("province", instance.getProvince(), MatchMode.ANYWHERE));
			}
			if (StringUtils.isNotEmpty(instance.getCity())) {
				dc.add(Restrictions.ilike("city", instance.getCity(), MatchMode.ANYWHERE));
			}
			if (Constants.DB_NONE != instance.getValidation()) {
				dc.add(Restrictions.eq("validation", new Integer(instance.getValidation())));
			}
			// 日期匹配从 beginDate 00:00:00 开始到 endDate 23:59:59，即 >= beginDate 00:00:00 and < endDate+1 00:00:00
			if (beginDate != null) {
				dc.add(Restrictions.ge("submitTime", beginDate));
			}
			if (endDate != null) {
				endDate = DateUtils.addDays(endDate, 1);
				dc.add(Restrictions.lt("submitTime", endDate));
			}

			return findByDetachedCriteria(dc);
		} catch (RuntimeException re) {
			log.error("search Data instance failed", re);
			throw re;
		}
	}

	/**
	 * @see com.xunchi.haichuan.data.dao.DataDAO#search(com.xunchi.haichuan.data.model.Data, java.util.Date,
	 *      java.util.Date, int, int)
	 */
	public List<Data> search(Data instance, Date beginDate, Date endDate, int start, int limit) {
		log.debug("searching Data instance");
		try {
			DetachedCriteria dc = getEntityDetachedCriteria();
			if (StringUtils.isNotEmpty(instance.getProvince())) {
				dc.add(Restrictions.like("province", instance.getProvince(), MatchMode.ANYWHERE));
			}
			if (StringUtils.isNotEmpty(instance.getCity())) {
				dc.add(Restrictions.ilike("city", instance.getCity(), MatchMode.ANYWHERE));
			}
			if (Constants.DB_NONE != instance.getValidation()) {
				dc.add(Restrictions.eq("validation", new Integer(instance.getValidation())));
			}
			// 日期匹配从 beginDate 00:00:00 开始到 endDate 23:59:59，即 >= beginDate 00:00:00 and < endDate+1 00:00:00
			if (beginDate != null) {
				dc.add(Restrictions.ge("submitTime", beginDate));
			}
			if (endDate != null) {
				endDate = DateUtils.addDays(endDate, 1);
				dc.add(Restrictions.lt("submitTime", endDate));
			}

			Criteria criteria = dc.getExecutableCriteria(getSession());
			return criteria.setFirstResult(start).setMaxResults(limit).list();
		} catch (RuntimeException re) {
			log.error("search Data instance failed", re);
			throw re;
		}
	}

	/**
	 * @see com.xunchi.haichuan.data.dao.DataDAO#checkDataName(java.lang.String)
	 */
	public boolean checkDataName(String dataName) throws Exception {
		String hql = "from Data where name = ?";
		List<Data> list = findByHql(hql, dataName);
		return !list.isEmpty();
	}

}
